package Presentation;

import BusinessLogic.*;
import static Presentation.frmClient.groupchat;
import java.awt.Toolkit;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

public class frmInvite extends javax.swing.JFrame {

    private final DefaultListModel modelShow = new DefaultListModel();
    private final DefaultListModel modelInvite = new DefaultListModel();
    private final DefaultListModel modelGroup = new DefaultListModel();
    private String name;
    private String group;
    
    public frmInvite() {
        initComponents();
        this.setLocation(500, 100);
        setIcon();
        ls_friend.setModel(modelShow);
        ls_invitefriend.setModel(modelInvite);
        StatusImage.Image(ls_friend);
    }
    
    private void setIcon() {
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/Images/icon.png")));
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pan_main = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        pan_friend = new javax.swing.JPanel();
        scr_friend = new javax.swing.JScrollPane();
        ls_friend = new javax.swing.JList();
        pan_Invite = new javax.swing.JPanel();
        src_Invite = new javax.swing.JScrollPane();
        ls_invitefriend = new javax.swing.JList();
        btn_Add = new javax.swing.JButton();
        btn_Delete = new javax.swing.JButton();
        btn_Invite = new javax.swing.JButton();
        btn_Cancel = new javax.swing.JButton();
        lbl_Username = new javax.swing.JLabel();

        setTitle("Mời thành viên vào nhóm");
        setResizable(false);

        pan_main.setAutoscrolls(true);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 0, 255));
        jLabel1.setText("Người mời: ");

        ls_friend.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        ls_friend.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        ls_friend.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ls_friendMouseClicked(evt);
            }
        });
        scr_friend.setViewportView(ls_friend);

        javax.swing.GroupLayout pan_friendLayout = new javax.swing.GroupLayout(pan_friend);
        pan_friend.setLayout(pan_friendLayout);
        pan_friendLayout.setHorizontalGroup(
            pan_friendLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scr_friend, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
        );
        pan_friendLayout.setVerticalGroup(
            pan_friendLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pan_friendLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scr_friend, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE))
        );

        ls_invitefriend.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        ls_invitefriend.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        ls_invitefriend.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ls_invitefriendMouseClicked(evt);
            }
        });
        src_Invite.setViewportView(ls_invitefriend);

        javax.swing.GroupLayout pan_InviteLayout = new javax.swing.GroupLayout(pan_Invite);
        pan_Invite.setLayout(pan_InviteLayout);
        pan_InviteLayout.setHorizontalGroup(
            pan_InviteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(src_Invite, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
        );
        pan_InviteLayout.setVerticalGroup(
            pan_InviteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pan_InviteLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(src_Invite, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE))
        );

        btn_Add.setBackground(new java.awt.Color(204, 204, 255));
        btn_Add.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        btn_Add.setText("Thêm >>");
        btn_Add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_AddActionPerformed(evt);
            }
        });

        btn_Delete.setBackground(new java.awt.Color(204, 204, 255));
        btn_Delete.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        btn_Delete.setText("<< Xóa");
        btn_Delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_DeleteActionPerformed(evt);
            }
        });

        btn_Invite.setBackground(new java.awt.Color(204, 204, 255));
        btn_Invite.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        btn_Invite.setText("Mời");
        btn_Invite.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_InviteActionPerformed(evt);
            }
        });

        btn_Cancel.setBackground(new java.awt.Color(204, 204, 255));
        btn_Cancel.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        btn_Cancel.setText("Đóng");
        btn_Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_CancelActionPerformed(evt);
            }
        });

        lbl_Username.setForeground(new java.awt.Color(0, 0, 255));

        javax.swing.GroupLayout pan_mainLayout = new javax.swing.GroupLayout(pan_main);
        pan_main.setLayout(pan_mainLayout);
        pan_mainLayout.setHorizontalGroup(
            pan_mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pan_mainLayout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbl_Username, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(pan_mainLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pan_friend, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pan_mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btn_Delete, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_Add, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pan_Invite, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pan_mainLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btn_Invite, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addComponent(btn_Cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(112, 112, 112))
        );
        pan_mainLayout.setVerticalGroup(
            pan_mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pan_mainLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pan_mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(lbl_Username, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(pan_mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pan_mainLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(pan_mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(pan_friend, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(pan_Invite, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(pan_mainLayout.createSequentialGroup()
                        .addGap(73, 73, 73)
                        .addComponent(btn_Add, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btn_Delete, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pan_mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btn_Invite, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_Cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pan_main, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pan_main, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_CancelActionPerformed
         this.setVisible(false);
    }//GEN-LAST:event_btn_CancelActionPerformed

    private void btn_InviteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_InviteActionPerformed
        String friend;  
        if(modelInvite.getSize()>0){
              for(int i=0;i<modelInvite.getSize();i++){
                  friend=modelInvite.getElementAt(i).toString();
                  friend= friend.substring(1, friend.length());
                  SendData(Command.JOIN_GROUP_REQUEST+name+((char)001)+friend+((char)002)+group);
              }
        }
        this.setVisible(false);
    }//GEN-LAST:event_btn_InviteActionPerformed

    private void btn_AddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_AddActionPerformed
        if(ls_friend.getSelectedIndex()>=0){
            int[] selectedIx = ls_friend.getSelectedIndices();
            String[] ls=new String[selectedIx.length];
            for (int i = 0; i < selectedIx.length; i++) {
                ls[i]=modelShow.getElementAt(selectedIx[i]).toString();
            }
            for (String l : ls) {
                modelInvite.addElement(l);
                for(int j=0;j<modelShow.getSize();j++){
                    if(l.equals(modelShow.getElementAt(j).toString())){
                        modelShow.removeElementAt(j);
                    }
                }
            }
            StatusImage.Image(ls_invitefriend);
        }
    }//GEN-LAST:event_btn_AddActionPerformed

    private void btn_DeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_DeleteActionPerformed
        if(ls_invitefriend.getSelectedIndex()>=0){
            int[] selectedIx = ls_invitefriend.getSelectedIndices();
            String[] ls=new String[selectedIx.length];
            for (int i = 0; i < selectedIx.length; i++) {
                ls[i]=modelInvite.getElementAt(selectedIx[i]).toString();
            }
            for (String l : ls) {
                modelShow.addElement(l);
                for(int j=0;j<modelInvite.getSize();j++){
                    if(l.equals(modelInvite.getElementAt(j).toString())){
                        modelInvite.removeElementAt(j);
                    }
                }
            }
            StatusImage.Image(ls_friend);
        }
    }//GEN-LAST:event_btn_DeleteActionPerformed

    private void ls_friendMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ls_friendMouseClicked
        if (evt.getClickCount() == 2) {
            String choose;
            choose=ls_friend.getSelectedValue().toString();
            modelInvite.addElement(choose);
            modelShow.removeElement(choose);
            StatusImage.Image(ls_invitefriend);
        }
    }//GEN-LAST:event_ls_friendMouseClicked

    private void ls_invitefriendMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ls_invitefriendMouseClicked
        if (evt.getClickCount() == 2) {
            String choose;
            choose=ls_invitefriend.getSelectedValue().toString();
            modelInvite.removeElement(choose);
            modelShow.addElement(choose);
            StatusImage.Image(ls_friend);
        }
    }//GEN-LAST:event_ls_invitefriendMouseClicked

    private int getIDGroup(String gr){
        System.out.println(modelGroup.getSize());
        for(int i=0;i<modelGroup.getSize();i++){
            if(modelGroup.getElementAt(i).toString().equals(gr))
                return i;
        }
        return -1;
    }
    
    private String getname(String str){
        return str.substring(1, str.length());
    }
    
    private int getLocation(String str){
        int loca=-1;
        for(int i=0;i<modelShow.getSize();i++){
            if(modelShow.getElementAt(i).toString().equals(str))
                loca=i;
        }
        return loca;
    }

    public void setModelGroup(DefaultListModel e){
        for(int i=0;i<e.getSize();i++){
            modelGroup.addElement(e.getElementAt(i));
        }
    }
    
     public void setFriendList(Object[] list){
        for(Object a: list){
            modelShow.addElement(a.toString());
        }
    }
     
    public void setUserName(String myname){
        name = myname;
        lbl_Username.setText(name);
    }
    
    public void setGroup(String gr){
        group = gr;
    }
    
    private void SendData(String str){
        if(!Connection.sendData(str)){
            JOptionPane.showMessageDialog(rootPane,"Lỗi! Mất kết nối với server", "Thông kết nối", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Add;
    private javax.swing.JButton btn_Cancel;
    private javax.swing.JButton btn_Delete;
    private javax.swing.JButton btn_Invite;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lbl_Username;
    private javax.swing.JList ls_friend;
    private javax.swing.JList ls_invitefriend;
    private javax.swing.JPanel pan_Invite;
    private javax.swing.JPanel pan_friend;
    private javax.swing.JPanel pan_main;
    private javax.swing.JScrollPane scr_friend;
    private javax.swing.JScrollPane src_Invite;
    // End of variables declaration//GEN-END:variables
}
